﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace FeatureDemo.Helper
{
    public class DemoHelper
    {
        public void CreatePanorama(Panorama panorama)
        {
            panorama.Items.Clear();

            Dictionary<string, ListBox> featureLists = new Dictionary<string, ListBox>();
            Assembly assembly = Assembly.GetExecutingAssembly();

            foreach (var type in assembly.GetTypes())
            {
                if (type.Namespace.Contains(".Features"))
                {
                    int idx = type.Namespace.IndexOf(".Features") + 10;
                    if (idx >= type.Namespace.Length)
                    {
                        continue;
                    }
                    var featureArea = type.Namespace.Substring(idx);

                    ListBox lb = null;
                    if (featureLists.ContainsKey(featureArea))
                    {
                        lb = featureLists[featureArea];
                    }
                    else
                    {
                        lb = new ListBox();
                        lb.SelectionChanged += (s, e) =>
                        {
                            ListBox lbHelper = s as ListBox;
                            var si = lbHelper.SelectedItem;
                            StackPanel spHelp = si as StackPanel;

                            //TextBlock tb = si as TextBlock;
                            Type typeHelper = spHelp.Tag as Type;

                            string page;
                            if (typeHelper.FullName.Contains("FeatureOverview"))
                            {
                                page = typeHelper.FullName.Replace('.', '/').Replace("FeatureOverview", "") + ".xaml";
                            }
                            else
                            {
                                page = typeHelper.FullName.Replace('.', '/').Replace("FeatureDemo", "") + ".xaml";
                            }
                            Uri uri = new Uri(page, UriKind.Relative);
                            OnNavigateTo(uri);
                        };
                        featureLists.Add(featureArea, lb);
                    }

                    if (type.Name.StartsWith("<>"))
                    {
                        continue;
                    }

                    string title;
                    string description;
                    double version;

                    object[] attributes = type.GetCustomAttributes(typeof(FeatureAttribute), false);
                    if (attributes.Length>0)
                    {   
                        FeatureAttribute fa = ((FeatureAttribute)attributes[0]);
                        title = fa.FeatureName;
                        description = fa.Description;
                        version = fa.Version;
                    }
                    else
                    {
                        title = type.Name;
                        version = 7.0;
                        description = "<no description>";
                    }

                    StackPanel sp = new StackPanel();
                    sp.Tag = type;

                    TextBlock tbTitle = new TextBlock();
                    tbTitle.Text = title;
                    tbTitle.FontSize = 40;
                    sp.Children.Add(tbTitle);
                    
                    TextBlock tbDescription = new TextBlock();
                    tbDescription.Text = description + Environment.NewLine + "Version: " + version.ToString("0.0");
                    tbDescription.FontSize = 20;
                    sp.Children.Add(tbDescription);

                    lb.Items.Add(sp);
                }
            }

            #region sort lists

            foreach (var list in featureLists)
            {
                ListBox lb = list.Value as ListBox;
                foreach (var i in lb.Items)
                {

                }
            }

            #endregion

            var sortedFeatureLists = from i in featureLists
                                     orderby i.Key
                                     select i;

            foreach (var list in sortedFeatureLists)
            {
                PanoramaItem pi = new PanoramaItem();
                pi.Header = list.Key;
                pi.Content = list.Value;
                panorama.Items.Add(pi);
            }
        }

        private void OnNavigateTo(Uri uri)
        {
            if (NavigateTo != null)
            {
                DemoHelperEventArgs args = new DemoHelperEventArgs();
                args.Uri = uri;
                NavigateTo(this, args);
            }
        }

        public event EventHandler<DemoHelperEventArgs> NavigateTo;

        //void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    ListBox lb = sender as ListBox;
        //    var si = lb.SelectedItem;
        //    TextBlock tb = si as TextBlock;
        //    Type type = tb.Tag as Type;

        //    string page = type.FullName.Replace('.', '/').Replace("FeatureOverview", "") + ".xaml";
        //    Uri uri = new Uri(page, UriKind.Relative);
        //    NavigationService.Navigate(uri);
        //}
    }

    public class DemoHelperEventArgs : EventArgs
    {
        public Uri Uri { get; set; }
    }
}
